import os
import sys

from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QApplication
import cv2
from ui.show_comics import Ui_MainWindow


class ComicDisplay(QMainWindow, Ui_MainWindow):
    def __init__(self, chapter_name, chapter_dir, chapter_info, index):
        super(ComicDisplay, self).__init__()
        self.setupUi(self)
        self.label.clear()
        self.chapter_info = chapter_info
        self.chapter_name = chapter_name
        self.chapter_dir = chapter_dir
        self.chapter_index = index
        self.load_image()
        # self.pushButton_4.clicked.connect(self.previous_chapter)
        # self.pushButton_3.clicked.connect(self.next_chapter)

    # 加载漫画图片
    def load_image(self):
        # 显示标题
        self.label.setText(self.chapter_name)
        height = 0
        # 判断路径是否存在
        if os.path.exists(self.chapter_dir):
            image_list = os.listdir(self.chapter_dir)
            if not image_list:
                label_4 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
                self.verticalLayout_2.addWidget(label_4)
                label_5 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
                self.verticalLayout_2.addWidget(label_5)
                label_4.setScaledContents(True)
                label_5.setScaledContents(True)
                label_4.setText("加载失败")
                label_5.setText("加载失败")
                self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(0, height + 900))
            else:
                for imag_name in image_list:
                    imag_label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
                    self.verticalLayout_2.addWidget(imag_label)
                    imag_label.setScaledContents(True)
                    image_path = os.path.join(self.chapter_dir, imag_name)
                    image = cv2.imread(image_path)
                    imag_label.setMinimumSize(QtCore.QSize(480, image.shape[1]))
                    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                    height += image.shape[1]
                    image = QtGui.QImage(image.data, image.shape[1], image.shape[0], QtGui.QImage.Format_RGB888)
                    imag_label.setPixmap(QPixmap.fromImage(image))
                self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(378, height))

    # 上一章 self.pushButton_3
    def previous_chapter(self):
        if self.chapter_index <= 0:
            return
        else:
            self.hide()
            self.chapter_index -= 1
            self.chapter_dir = self.chapter_info[self.chapter_index][1]
            self.chapter_name = self.chapter_info[self.chapter_index][0]
            self.load_image()

    # 下一章 self.pushButton_4
    def next_chapter(self):
        if self.chapter_index >= len(self.chapter_info)-1:
            return
        else:
            self.hide()
            self.chapter_index += 1
            self.chapter_dir = self.chapter_info[self.chapter_index][1]
            self.chapter_name = self.chapter_info[self.chapter_index][0]
            dc = ComicDisplay(self.chapter_name, self.chapter_dir, self.chapter_index, self.chapter_info)
            dc.show()


